# -*- coding: utf-8 -*-
# @Time : 2024/2/3 10:04 AM 
# @Author : Wayne
# @Email : 1
# @File : dic_key_change.py
# @Project : young_python_demo
# @DESCRIPTION : 数组中的字典key 批量转换

"""
返回数组字典中的数据，通过特定的字段转换为需要的 key
比如源头的 key 到客户目标的 key
"""
json_array = [
    {
        "a": "01",
        "b": 3,
        "c": 4
    },
    {
        "a": "05",
        "b": 65,
        "c": 666
    }
]
field_mappings = {"a": "num", "b": "age", "d": "name"}


def extract_and_rename_fields_from_array(json_array, field_mappings):
    extracted_array = []
    for item in json_array:
        extracted_item = {}
        for source_field, target_field in field_mappings.items():
            if source_field in item:
                extracted_item[target_field] = item[source_field]
            else:
                extracted_item[target_field] = ""
        extracted_array.append(extracted_item)
    return extracted_array
